﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CorpAbreu.Common.BaseClasses;
using System.Windows.Input;
using System.Collections.ObjectModel;
using Microsoft.Practices.Prism.Events;
using CorpAbreu.Common;


namespace MenuBarModule.ViewModels
{
    public class MenuBarViewModel : ViewModelBase
    {

        public IList<MenuItem> MenuItems { get; private set; }
        private readonly IEventAggregator _eventAggregator;

        public MenuBarViewModel(IEventAggregator eventAggregator)
        {
            this._eventAggregator = eventAggregator;
            this._eventAggregator.GetEvent<TesteSelectedEvent>().Publish("STOCK0");

            this.MenuItems = new List<MenuItem>();

            this.MenuItems.Add(new MenuItem
            {
                Text = "Ficheiro",
                Children = new ObservableCollection<MenuItem> {
                    new MenuItem{Text="Novo",Command=Novo},
                    new MenuItem{Text="Editar",Command=Editar},
                    new MenuItem{Text="Sair",Command=Sair}
            }
            });
            this.MenuItems.Add(new MenuItem
            {
                Text = "Utilizador",Children = new ObservableCollection<MenuItem> {
                    new MenuItem{Text="Mudar Utilizador",Command=ChangeUser}
                }
            });
        }

        public ICommand Novo
        {
            get { return new Microsoft.Practices.Prism.Commands.DelegateCommand(NovoClick); }
        }

        public ICommand Editar
        {
            get { return new Microsoft.Practices.Prism.Commands.DelegateCommand(EditarClick); }
        }

        public ICommand Sair
        {
            get { return new Microsoft.Practices.Prism.Commands.DelegateCommand(Quit); }
        }

        public ICommand ChangeUser
        {
            get { return new Microsoft.Practices.Prism.Commands.DelegateCommand(ChangeUserClick); }
        }

        public void NovoClick()
        {
            //this._eventAggregator.GetEvent<TesteSelectedEvent>().Publish("STOCK01");

            this._eventAggregator.GetEvent<MenuCommandSelected>().Publish("Novo");
        }

        public void EditarClick()
        {
            //this._eventAggregator.GetEvent<TesteSelectedEvent>().Publish("STOCK01");

            this._eventAggregator.GetEvent<MenuCommandSelected>().Publish("Editar");
        }

        public void Quit()
        {
            //this._eventAggregator.GetEvent<TesteSelectedEvent>().Publish("STOCK01");

            this._eventAggregator.GetEvent<MenuCommandSelected>().Publish("Sair");
        }

        public void ChangeUserClick()
        {
            //this._eventAggregator.GetEvent<TesteSelectedEvent>().Publish("STOCK01");
            this._eventAggregator.GetEvent<ChangeUserSelected>().Publish("ChangeUser");
            //this._eventAggregator.GetEvent<MenuCommandSelected>().Publish("ChangeUser");
        }
        
    }

    public class MenuItem : ViewModelBase
    {
        private string _Text;
        public string Text
        {
            get { return _Text; }
            set
            {
                if (value != _Text)
                {
                    _Text = value;
                    OnPropertyChanged("Text");
                }
            }
        }

        private Uri _imageSource;
        public Uri imageSource
        {
            get { return _imageSource; }
            set
            {
                if (value != _imageSource)
                {
                    _imageSource = value;
                    OnPropertyChanged("imageSource");
                }
            }
        }

        private ICommand _Command;
        public ICommand Command
        {
            get { return _Command; }
            set
            {
                if (value != _Command)
                {
                    _Command = value;
                    OnPropertyChanged("Command");
                }
            }
        }

        private ObservableCollection<MenuItem> _Children;
        public ObservableCollection<MenuItem> Children
        {
            get { return _Children; }
            set
            {
                if (value != _Children)
                {
                    _Children = value;
                    OnPropertyChanged("Children");
                }
            }
        }

    }
}
